import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    hasLogin: false,
    userInfo: {},
    deviceDatas: {}
  },
  mutations: {
    login(state, provider) {
      state.hasLogin = true
      state.userInfo = provider
      uni.setStorageSync('userInfo', provider)
    },
    logout(state) {
      state.hasLogin = false
      state.userInfo = {}
      uni.removeStorageSync('userInfo')
    },
    updateDeviceData(state, deviceDatas) {
      for (var prop in deviceDatas) {
        const dataPoint = deviceDatas[prop]
        if (dataPoint.type === 1) {
          dataPoint.value = parseInt(dataPoint.value)
        } else if (dataPoint.type === 3) {
          dataPoint.value = parseFloat(dataPoint.value)
        } else if (dataPoint.type === 4) {
          dataPoint.value = dataPoint.value === 'true'
        }
      }
      state.deviceDatas = deviceDatas
    },
    updateDeviceDataSingle(state, dataPoint) {
      if (dataPoint.type === 1) {
        dataPoint.value = parseInt(dataPoint.value)
      } else if (dataPoint.type === 3) {
        dataPoint.value = parseFloat(dataPoint.value)
      } else if (dataPoint.type === 4) {
        dataPoint.value = dataPoint.value === 'true'
      }
      state.deviceDatas[dataPoint.idTag] = dataPoint
    }
  }
})

export default store
